﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Nhom16_PTTKHTTT_12HCB
{
    public partial class frmLenDanhSachMonAn : Form
    {
        public frmLenDanhSachMonAn()
        {
            InitializeComponent();
            DanhSachLoaiMon();
        }
        LoaiMonAn_BUS lm_bus = new LoaiMonAn_BUS();
        private void txtThem_PN_Click(object sender, EventArgs e)
        {
            LoaiMonAn lm = new LoaiMonAn();
            lm.TenLoaiMonAn = txtTenLoaiMon.Text;
            lm.IsDelete = "true";
            if (lm_bus.ThemLoaiMon(lm) == true)
            {
                MessageBox.Show("Thêm thành công");
                DanhSachLoaiMon();
                LoadLoaiMonAn();
            }
        }

        void DanhSachLoaiMon()
        {
            DataTable dt = new DataTable();
            dt = lm_bus.DanhSachLoaiMon();
            dgvLoaiMon.AutoGenerateColumns = false;
            dgvLoaiMon.DataSource = dt;
        }

        private void dgvLoaiMon_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
             dgvLoaiMon.Rows[e.RowIndex].Cells["STTs"].Value = e.RowIndex + 1;
        }

        private void btnXoa_LoaiMon_Click(object sender, EventArgs e)
        {
            string ma = dgvLoaiMon.CurrentRow.Cells[1].Value.ToString();
            lm_bus.XoaLoaiMon(ma);
            DanhSachLoaiMon();
        }

        private void btnSuaLoaiMon_Click(object sender, EventArgs e)
        {
            LoaiMonAn lm = new LoaiMonAn();
            lm.MaLoaiMonAn = txtMaLoaiMon.Text;
            lm.TenLoaiMonAn = txtTenLoaiMon.Text;
            lm_bus.SuaLoaiMon(lm);
            DanhSachLoaiMon();
        }

        private void dgvLoaiMon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string ma = dgvLoaiMon.CurrentRow.Cells[1].Value.ToString();
            txtMaLoaiMon.Text = ma;
            txtTenLoaiMon.Text = dgvLoaiMon.CurrentRow.Cells[2].Value.ToString();
        }

        private void btnThoatLoaiMon_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LoadLoaiMonAn()
        {
            lm_bus = new LoaiMonAn_BUS();
            cbxLoaiMon.DataSource = lm_bus.DanhSachLoaiMon();
            cbxLoaiMon.DisplayMember = "TenLoaiMonAn";
            cbxLoaiMon.ValueMember = "MaLoaiMonAn";
        }

        void LoadDSNguyenVatLieu()
        {
            NguyenVatLieu_BUS nvl_bus = new NguyenVatLieu_BUS();
            cbxTenNguyenVatLieu.DisplayMember = "TenNVL";
            cbxTenNguyenVatLieu.ValueMember = "MaNVL";
            cbxTenNguyenVatLieu.DataSource = nvl_bus.DanhSachNVL();
            
        }
        private void frmLenDanhSachMonAn_Load(object sender, EventArgs e)
        {
            LoadLoaiMonAn();
            LoadDSNguyenVatLieu();
            DanhSachMonAn();
        }

        private void dtgLoaiNVL_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvChiTietMon.Rows[e.RowIndex].Cells["STT"].Value = e.RowIndex + 1;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (active.Equals("ChiTiet"))
            {
                if (txtSoLuong.Text.Equals(""))
                    MessageBox.Show("Vui lòng nhập số lượng");
                else
                {
                    MonAn_NVL manvl = new MonAn_NVL();
                    manvl.MaMonAn = dgvMonAn.CurrentRow.Cells[1].Value.ToString();
                    manvl.MaNVL = cbxTenNguyenVatLieu.SelectedValue.ToString();
                    manvl.SoLuong = int.Parse(txtSoLuong.Text);
                    MonAn_BUS mab = new MonAn_BUS();
                    if (mab.ThemMonAn_NVL(manvl))
                    {

                        DataTable dt = new DataTable();
                        dt = mab.DanhSachMonAn_Ma(manvl.MaMonAn);
                        dgvChiTietMon.DataSource = dt;
                    }
                    else
                    {
                        MessageBox.Show("thất bại", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else 
            if(active.Equals("ko"))
            {
                if (txtSoLuong.Text.Equals(""))
                    MessageBox.Show("Vui lòng nhập số lượng");
                else
                {
                    int flag = -1;
                    for (int i = 0; i < dgvChiTietMon.Rows.Count; i++)
                    {
                        if (dgvChiTietMon.Rows[i].Cells[1].Value.ToString() == cbxTenNguyenVatLieu.SelectedValue.ToString())
                        {
                            flag = i;
                        }
                    }
                    if (flag != -1)
                    {
                        int sl = int.Parse(dgvChiTietMon.Rows[flag].Cells[3].Value.ToString());
                        sl = sl + int.Parse(txtSoLuong.Text);
                        dgvChiTietMon.Rows[flag].Cells[3].Value = sl;
                    }
                    else
                    {
                        dgvChiTietMon.Rows.Add("", cbxTenNguyenVatLieu.SelectedValue, cbxTenNguyenVatLieu.Text, txtSoLuong.Text);
                    }
                }
            }
           
            

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {

                MonAn_BUS ma_bus = new MonAn_BUS();
                dgvChiTietMon.Rows.RemoveAt(dgvChiTietMon.CurrentRow.Index);
                ma_bus.XoaMonAn_NVL(dgvChiTietMon.CurrentRow.Cells[1].Value.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Món ăn phải có nguyên vật liệu");
                
            }
            
        }

        private void dgvMonAn_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvLoaiMon.Rows[e.RowIndex].Cells["STTMon"].Value = e.RowIndex + 1;
        }

        public void DanhSachMonAn()
        {
            DataTable dt = new DataTable();
            MonAn_BUS ma = new MonAn_BUS();
            dgvMonAn.AutoGenerateColumns = false;
            dgvMonAn.DataSource = ma.DanhSachMonAn();
        }

        private void btnThemMon_Click(object sender, EventArgs e)
        {
            try
            {
                MonAn_BUS ma_bus = new MonAn_BUS();
                MonAn ma_dto = new MonAn();
                string maMon = ma_bus.MaNVLTang();
                ma_dto.MaMonAn = maMon;
                ma_dto.MaLoaiMonAn = cbxLoaiMon.SelectedValue.ToString();
                ma_dto.TenMonAn = txtTenMonAn.Text;
                ma_dto.Gia = decimal.Parse(txtGia.Text);
                if (ma_bus.ThemMonAn(ma_dto) == true)
                {
                    for (int i = 0; i < dgvChiTietMon.Rows.Count; i++)
                    {
                        MonAn_NVL monannvl_dto = new MonAn_NVL();
                        monannvl_dto.MaMonAn = maMon;
                        monannvl_dto.MaNVL = dgvChiTietMon.Rows[i].Cells[1].Value.ToString();
                        monannvl_dto.SoLuong = int.Parse(dgvChiTietMon.Rows[i].Cells[3].Value.ToString());
                        ma_bus.ThemMonAn_NVL(monannvl_dto);
                    }
                    MessageBox.Show("Thêm món ăn thành công");
                    DanhSachMonAn();
                }
            }
            catch (Exception ex)
            { 
            }
            
        }

        private void dgvMonAn_RowPrePaint_1(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvMonAn.Rows[e.RowIndex].Cells["STTMonAn"].Value = e.RowIndex + 1;
        }

        private void btnXoaMon_Click(object sender, EventArgs e)
        {
            MonAn_BUS ma_bus = new MonAn_BUS();
            string ma = dgvMonAn.CurrentRow.Cells[1].Value.ToString();
            if (ma_bus.XoaMon(ma) == true)
            {
                MessageBox.Show("Xóa thành công");
                DanhSachMonAn();
            }
        }
        
        private void dgvMonAn_SelectionChanged(object sender, EventArgs e)
        {
            
            //MessageBox.Show(ma.ToString());
        }

        string active = "ko";
        private void dgvMonAn_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string ma = dgvMonAn.CurrentRow.Cells[1].Value.ToString();
            MonAn_BUS ma_bus = new MonAn_BUS();
            DataTable dt = new DataTable();
            dt = ma_bus.DanhSachMonAn_Ma(ma);
            txtMaMonAn.Text = dt.Rows[0][0].ToString();
            txtTenMonAn.Text = dt.Rows[0][4].ToString();
            cbxLoaiMon.Text = dt.Rows[0][5].ToString();
            txtGia.Text = dt.Rows[0][6].ToString();
            dgvChiTietMon.AutoGenerateColumns = false;
            dgvChiTietMon.DataSource = dt;
            active = "ChiTiet";
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            txtGia.Text = "";
            txtMaMonAn.Text = "";
            txtSoLuong.Text = "";
            txtTenMonAn.Text = "";
            txtSoLuong.Text = "";
            active = "ko";
            dgvChiTietMon.DataSource = null;
        }

        private void btnSuaMon_Click(object sender, EventArgs e)
        {
            MonAn ma_dto = new MonAn();
            MonAn_BUS ma_bus = new MonAn_BUS();
            ma_dto.MaMonAn = txtMaMonAn.Text;
            ma_dto.TenMonAn = txtTenMonAn.Text;
            ma_dto.MaLoaiMonAn = cbxLoaiMon.SelectedValue.ToString();
            ma_dto.Gia = decimal.Parse(txtGia.Text);
            if (ma_bus.SuaMonAn(ma_dto) == true)
            {
                MessageBox.Show("Sửa thành công");
                DanhSachMonAn();
            }

        }
        
        
        private void cbxTenNguyenVatLieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = cbxTenNguyenVatLieu.SelectedValue.ToString();
                //MessageBox.Show(str);
                //lay thuoc tinh don vi tinh
                NguyenVatLieu_BUS nvl_bus = new NguyenVatLieu_BUS();
                NguyenVatLieu nvl = new NguyenVatLieu();
                System.Data.DataTable dt = new DataTable();
                dt = nvl_bus.DanhSachNVL_Ma(cbxTenNguyenVatLieu.SelectedValue.ToString());
                lblDVT.Text = dt.Rows[0].ItemArray[4].ToString();
            }
            catch (Exception ex)
            { 
            }
 
        }

    }
}
